package com.gbr.application;

import com.ng.framework.service.preference.Preference;
import com.ng.framework.service.preference.IPreferenceService;

import java.io.File;
import java.io.Serializable;

/**
 * System Preferences.
 * 
 * @author Greg Rodgers
 * 
 */
public class AnimatedGifPreferences implements Serializable {
	/** Serialized UID *. */
	private static final long serialVersionUID = -4659913886224089098L;
	
	/** The Constant Name. */
	public static final String Name = "System";
	public static final String TrackWindowState = "TrackWindowPositions";
	public static final String WorkingFolderLocation = "WorkingFolderLocation";
	public static final String RecordingArea_X = "RecordingArea_X";
	public static final String RecordingArea_Y = "RecordingArea_Y";
	public static final String RecordingArea_WIDTH = "RecordingArea_WIDTH";
	public static final String RecordingArea_HEIGHT = "RecordingArea_HEIGHT";

	/**
	 * Check to see if these preferences exist.
	 * 
	 * @param service the IPreferenceService
	 * @return if preference exists
	 */
	public static boolean exists(IPreferenceService service) {
		return service.getSystemPreference(Name) != null;
	}

	/**
	 * Create a new preference and set the defaults.
	 * 
	 * @param service the IPreferenceService
	 */
	public static void create(IPreferenceService service) {
		final Preference pref = new Preference(Name);
		service.setSystemPreference(pref);
		setDefaults(service);
	}

	/**
	 * Set the defaults for this preference.
	 * 
	 * @param service the IPreferenceService
	 */
	public static void setDefaults(IPreferenceService service) {
		final Preference pref = service.getSystemPreference(Name);
		pref.set(TrackWindowState, Defaults.TrackWindowState);
		pref.set(WorkingFolderLocation, Defaults.WorkingFolderLocation);
		pref.set(RecordingArea_X, Defaults.RecordingArea_X);
		pref.set(RecordingArea_Y, Defaults.RecordingArea_Y);
		pref.set(RecordingArea_WIDTH, Defaults.RecordingArea_WIDTH);
		pref.set(RecordingArea_HEIGHT, Defaults.RecordingArea_HEIGHT);
	}
	
	/**
	 * The default values for this preference.
	 * 
	 * @author Greg Rodgers
	 *
	 */
	public static class Defaults {
		public static final boolean TrackWindowState = true;
		public static String WorkingFolderLocation ="";
		public static int RecordingArea_X = 0;
		public static int RecordingArea_Y = 0;
		public static int RecordingArea_WIDTH = 0;
		public static int RecordingArea_HEIGHT = 0;
		
		//find the working folder either in user home or temporary file storage
		static { 
		   String[] working = new String[]{
		      System.getProperty("user.home"), 
		      System.getProperty("java.io.tmpdir")};
		   for(String work: working) {
		      File f = new File(work);
		      if(f.exists()) {
		         WorkingFolderLocation = work;
		         break;
		      }
		   }
		   if(WorkingFolderLocation.isEmpty()) {
		      WorkingFolderLocation = System.getProperty("file.separator");
		   }
		}
	}
}
